<?php
include_once("../includes/global.php");
include_once("../includes/page_utf_class.php");
$script_tmp = explode('/', $_SERVER['SCRIPT_NAME']);
$sctiptName = array_pop($script_tmp);
include_once("auth.php");
//======================================
if(!empty($_POST["submit"]))
{
	foreach($_POST['id'] as $key=>$v)
	{
		$sql="update ".PROVINCE." set
		province='".$_POST['provinces'][$key]."',nums='".$_POST['nums'][$key]."',country_id='".$_POST['country_id'][$key]."' 
		where province_id='$v'";
		$db->query($sql);
		if($_POST['delid'][$key])
		{
			$db->query("delete from ".PROVINCE." where province_id='".$_POST['delid'][$key]."'");
		}
	}
	if(!empty($_POST['add_provinces']))
	{
		$sql="insert into ".PROVINCE." (province,nums,country_id) 
		values 
		('$_POST[add_provinces]','$_POST[add_nums]','$_POST[add_country_id]')";
		$db->query($sql);
	}
	admin_msg('province.php','Successful');
}

$sql="select * from ".COUNTRY." order by nums asc,ename asc";
$db->query($sql);
$ct=$db->getRows();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<TITLE><?php echo lang_show('admin_system');?></TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="main.css" rel="stylesheet" type="text/css" />
</HEAD>
<body>
<div class="guidebox"><?php echo lang_show('sys_m');?> &raquo; <?php echo lang_show('provin_m');?></div>
<div class="bigbox">
	<div class="bigboxhead"><?php echo lang_show('provin_m');?></div>
	<div class="bigboxbody">
	<form action="" method="post">
          <table width="100%" border="0" cellpadding="0" cellspacing="0">
            <tr class="theader">
			  <td width="30">删除</td>
			  <td width="30">排序</td>
              <td width="250"><?php echo lang_show('p_n');?></td>
              <td colspan="2">
			  	<select onchange="window.location='province.php?country_id='+this.value;" class="select" name="country" id="country">
				<option value="" selected></option>
				<?php
				foreach($ct as $v)
				{
				?>
				   <option <?php if($v['id']==$_GET['country_id']){?>selected="selected"<?php } ?> value="<?php echo $v['id']; ?>"><?php  echo $v['ename']; ?></option>
				 <?php } ?>
				</select>
				</td>
            </tr>
		<?php
		if(isset($_GET["country_id"]))
			$ksql.=" and country_id='$_GET[country_id]' ";
		$sql="select * from ".PROVINCE." where 1=1 $ksql order by nums asc";
		$db->query($sql);
		while($db->next_record())
		{
		?>
            <tr>
			  <td>
			  <input type="checkbox" name="delid[]" value="<?php  echo $db->f('province_id'); ?>" />
			  <input name="id[]" type="hidden" value="<?php  echo $db->f('province_id'); ?>" />
			  </td>
			  <td><input style="width:30px;" value="<?php  echo $db->f('nums'); ?>" name="nums[]" type="text" /></td>
              <td><input class="text" value="<?php  echo $db->f('province'); ?>" name="provinces[]" type="text" /></td>
              <td colspan="2" align="left">
			  <select class="select" name="country_id[]" id="country_id">
				<option value="" selected><?php echo lang_show('select_country'); ?></option>
				<?php
				foreach($ct as $v)
				{
				?>
				   <option <?php if($v['id']==$db->f('country_id')){?>selected="selected"<?php } ?> value="<?php echo $v['id']; ?>"><?php  echo $v['ename']; ?></option>
				 <?php } ?>
				</select>
			  </td>
            </tr>
       <?php
		}
		?>
		<tr>
		<td>新增</td>
		<td><input style="width:30px;" name="add_nums" type="text" id="add_nums" value="0" class="text"></td>
		<td><input name="add_provinces" type="text" id="add_provinces" class="text"></td>
		<td colspan="2"><select class="select" name="add_country_id" id="add_country_id">
			<?php
			foreach($ct as $v)
			{
			?>
            <option <?php if($v['id']==$_GET['country_id']){?>selected="selected"<?php } ?> value="<?php echo $v['id']; ?>"><?php  echo $v['ename']; ?> <?php  echo $v['cname']; ?></option>
            <?php  }?>
		  </select></td>
		</tr>
		<tr><td></td><td colspan="4"><input class="btn" type="submit" name="submit" value="<?php echo lang_show('submit');?>"></td></tr>
	  </table>
	  </form>
  </div>
</div>
</body>
</html>